package com.laiyizhan.common.listener;

import com.laiyizhan.common.Constants;
import com.laiyizhan.common.utils.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * web启动时的监听器
 * Created by folie
 */
public class WebListener implements ServletContextListener {

    private static final Logger log = LoggerFactory.getLogger(WebListener.class);
    /**
     * 版本号
     */
    public static long version = 0;

    public void contextDestroyed(ServletContextEvent event) {
        log.info("------{Web App Stopping}----");
    }

    /**
     * 站点启动--初始化
     */
    public void contextInitialized(ServletContextEvent event) {
        try {
            // 初始化生成当前版本号
            version = System.currentTimeMillis() / 60000;
            event.getServletContext().setAttribute("version", version);
            event.getServletContext().setAttribute("debug", Constants.RD.equalsIgnoreCase(Configuration.getProperty("env")));
            log.info("------{Web App Starting}---- version: " + version);
            // 初始化 domain
            event.getServletContext().setAttribute("domain", Configuration.getProperty("domain"));

            if (Constants.RD.equalsIgnoreCase(Configuration.getProperty("env"))) {
                // 本地环境访问本地资源
                event.getServletContext().setAttribute("cdn", "");// 默认无值
            } else if (Constants.TCC.equalsIgnoreCase(Configuration.getProperty("env"))) {
                event.getServletContext().setAttribute("cdn", "");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            System.exit(-1);
        }
    }

}